package edu.cs.usask.ca.silver.plugin.rcp.dialogs.create;

import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.cs.usask.ca.silver.plugin.rcp.dao.SilverBusinessLogicController;

/**
 * The create user dialog.
 * 
 * @author Yudi Xue
 * 
 */
public class CreateUserDialog extends TitleAreaDialog {

	private Text firstNameText;
	private Text lastNameText;
	private Text emailText;
	private Text passwordText;
	private Text confirmPasswordText;
	private String firstName;
	private String lastName;
	private String email;
	private String password;

	public CreateUserDialog(Shell parentShell) {
		super(parentShell);
	}

	@Override
	public void create() {
		super.create();
		setTitle("Create New User");
		setMessage("register a new user to Silver",
				IMessageProvider.INFORMATION);
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		// layout.horizontalAlignment = GridData.FILL;
		parent.setLayout(layout);

		// The text fields will grow with the size of the dialog
		Group group = new Group(parent, SWT.NONE);
		GridData gridData = new GridData();
		gridData.horizontalIndent = 2;
		gridData.horizontalSpan = 2;
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		group.setLayoutData(gridData);
		group.setLayout(new GridLayout(2, false));

		GridData gridLabelData = new GridData();

		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = SWT.FILL;
		// gridData.grabExcessVerticalSpace = true;

		Label label1 = new Label(group, SWT.NONE);
		label1.setText("First Name");
		label1.setLayoutData(gridLabelData);
		firstNameText = new Text(group, SWT.BORDER);
		firstNameText.setLayoutData(gridData);

		Label label2 = new Label(group, SWT.NONE);
		label2.setText("Last Name");
		label2.setLayoutData(gridLabelData);
		lastNameText = new Text(group, SWT.BORDER);
		lastNameText.setLayoutData(gridData);

		Label label3 = new Label(group, SWT.NONE);
		label3.setText("Email (login)");
		label3.setLayoutData(gridLabelData);
		emailText = new Text(group, SWT.BORDER);
		emailText.setLayoutData(gridData);

		Label label4 = new Label(group, SWT.NONE);
		label4.setText("Password");
		label4.setLayoutData(gridLabelData);
		int style = SWT.PASSWORD | SWT.BORDER;
		passwordText = new Text(group, style);
		passwordText.setLayoutData(gridData);

		Label label5 = new Label(group, SWT.NONE);
		label5.setLayoutData(gridLabelData);
		label5.setText("Confirm Password");
		confirmPasswordText = new Text(group, style);
		confirmPasswordText.setLayoutData(gridData);

		return parent;
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		GridData gridData = new GridData();
		gridData.verticalAlignment = GridData.FILL;
		gridData.horizontalSpan = 3;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = SWT.CENTER;

		parent.setLayoutData(gridData);
		// Create Add button
		// Own method as we need to overview the SelectionAdapter
		createOkButton(parent, OK, "Register Me", true);
		// Add a SelectionListener

		// Create Cancel button
		Button cancelButton = createButton(parent, CANCEL, "Cancel", false);
		// Add a SelectionListener
		cancelButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				setReturnCode(CANCEL);
				close();
			}
		});
	}

	protected Button createOkButton(Composite parent, int id, String label,
			boolean defaultButton) {
		// increment the number of columns in the button bar
		((GridLayout) parent.getLayout()).numColumns++;
		Button button = new Button(parent, SWT.PUSH);
		button.setText(label);
		button.setFont(JFaceResources.getDialogFont());
		button.setData(Integer.valueOf(id));
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				if (isValidInput()) {
					okPressed();
				}
			}
		});
		if (defaultButton) {
			Shell shell = parent.getShell();
			if (shell != null) {
				shell.setDefaultButton(button);
			}
		}
		setButtonLayoutData(button);
		return button;
	}

	private boolean isValidInput() {
		boolean valid = true;
		if (firstNameText.getText().length() == 0) {
			setErrorMessage("Please give your first name");
			valid = false;
		}

		if (SilverBusinessLogicController
				.checkExceeding128CharStringLength(firstNameText.getText())) {
			setErrorMessage("first Name is too long");
			valid = false;
		}

		if (lastNameText.getText().length() == 0) {
			setErrorMessage("Please give your last name");
			valid = false;
		}

		if (SilverBusinessLogicController
				.checkExceeding128CharStringLength(lastNameText.getText())) {
			setErrorMessage("last Name is too long");
			valid = false;
		}

		if (SilverBusinessLogicController
				.checkExceeding500CharStringLength(emailText.getText())) {
			setErrorMessage("email is too long");
			valid = false;
		}

		if (passwordText.getText().length() == 0) {
			setErrorMessage("Please specify a password");
			valid = false;
		}

		if (confirmPasswordText.getText().compareTo((passwordText.getText())) != 0) {
			setErrorMessage("The confirmed password is not correct");
			confirmPasswordText.clearSelection();
			valid = false;
		}
		if (SilverBusinessLogicController
				.checkExceeding1000CharStringLength(passwordText.getText())) {
			setErrorMessage("password is too long");
			valid = false;
		}
		return valid;
	}

	// We allow the user to resize this dialog
	@Override
	protected boolean isResizable() {
		return true;
	}

	// We need to have the textFields into Strings because the UI gets disposed
	// and the Text Fields are not accessible any more.
	private void saveInput() {
		firstName = firstNameText.getText();
		lastName = lastNameText.getText();
		email = emailText.getText();
		password = passwordText.getText();
	}

	@Override
	protected void okPressed() {
		saveInput();
		super.okPressed();
	}

	public String getFirstName() {
		return firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public String getEmail() {
		return email;
	}

	public String getPassWord() {
		return password;
	}

}
